import 'dart:io';

import 'package:flutter/widgets.dart';
import 'package:realmega_ota/realmega/ota_manager.dart';
import 'package:realmega_ota/realmega/running_log_manager.dart';
import 'package:realmega_ota/util/ble_manager.dart';

class AppStateObserver extends WidgetsBindingObserver {
  bool get isInBackground => _isInBackground;
  bool _isInBackground = false;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    RunningLogManager.instance.addLog('current app lifecycle state:$state');
    switch (state) {
      case AppLifecycleState.paused:
        RunningLogManager.instance.addLog('app in background');
        _isInBackground = true;
        OtaManager.instance.inBackground = true;
        OtaManager.instance.resetOta();
        exit(0);
        break;
      case AppLifecycleState.resumed:
        _isInBackground = false;
        OtaManager.instance.inBackground = false;
        // BleManager.startScan();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.hidden:
      case AppLifecycleState.detached:
        // TODO: Handle this case.
    }
  }
}